package com.chinawlx.wlxteacher.ui.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;


import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.ui.fragment.WLXLoadingFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者:黄帅
 * 时间:16/9/9
 * 描述: 播放视频界面
 */
public class WLXPlayVideoActivity extends WLXBaseActivity {
    @BindView(R.id.playVideo)
    VideoView mVideoView;

    public static final String VIDEO_URL = "video_url";
    private String mVideoUrl;//视频URL
    private WLXLoadingFragment mLoadingFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playvideo);
        ButterKnife.bind(this);

        init();
        mLoadingFragment = WLXLoadingFragment.getInstance();
        mLoadingFragment.show(getSupportFragmentManager(),"playvideo");
    }

    private void init() {
        initPassValue();
        initView();
    }

    //拿到传递的视频数据
    private void initPassValue() {
        Intent intent = getIntent();
        if (intent == null) {
            setResult(RESULT_CANCELED);
            finish();
            return;
        }
        mVideoUrl = intent.getStringExtra(VIDEO_URL);
        if (TextUtils.isEmpty(mVideoUrl)) {
            setResult(RESULT_CANCELED);
            finish();
            return;
        }
    }

    //初始化布局
    private void initView() {
        if (!TextUtils.isEmpty(mVideoUrl)) {
            Uri uri = Uri.parse(mVideoUrl);
            mVideoView.setVideoURI(uri);
            mVideoView.setMediaController(new MediaController(this));
            mVideoView.requestFocus();
            mVideoView.start();
            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mLoadingFragment.dismiss();
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        mVideoView.stopPlayback();
        finish();
    }

    @OnClick(R.id.iv_back)
    public void onClick(View view) {
        if (view.getId()==R.id.iv_back){
            onBackPressed();
        }
    }
}
